﻿using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.ServiceProcess;
using System.Windows.Forms;

namespace Service
{
    public partial class ServiceTool : Form
    {
        [DllImport("kernel32.dll")]
        public static extern Boolean AllocConsole();
        [DllImport("kernel32.dll")]
        public static extern Boolean FreeConsole();

        public ServiceTool()
        {
            InitializeComponent();
        }

        private void btnscan_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog ofd = new OpenFileDialog())
            {
                ofd.Filter = "可执行文件(*.exe)|*.exe";
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    txtpath.Text = ofd.FileName;
                }
            }
        }

        private void btnload_Click(object sender, EventArgs e)
        {
            //获得服务集合
            var serviceControllers = ServiceController.GetServices();
            cmbservices.DataSource = serviceControllers;
            cmbservices.DisplayMember = "ServiceName";
            cmbservices.ValueMember = "ServiceName";
        }

        private void btninstall_Click(object sender, EventArgs e)
        {
            var installPath = txtpath.Text;
            if (string.IsNullOrEmpty(installPath))
            {
                MessageBox.Show("请选择要安装的服务程序");
                btnscan.Focus();
                return;
            }

            if (!File.Exists(installPath))
            {
                MessageBox.Show("文件不存在");
                btnscan.Focus();
                return;
            }

            string cmd = @"C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe ";

            using (Process p = new Process())
            {
                p.StartInfo.FileName = "cmd.exe";
                p.StartInfo.UseShellExecute = false;    //是否使用操作系统shell启动
                p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
                p.StartInfo.RedirectStandardError = true;//重定向标准错误输出

                p.Start();//启动程序

                p.StandardInput.AutoFlush = true;
                p.StandardInput.WriteLine(cmd + installPath);
            }
        }

        private void btnuninstall_Click(object sender, EventArgs e)
        {
            using (Process p = new Process())
            {
                p.StartInfo.FileName = "cmd.exe";
                p.StartInfo.UseShellExecute = false;    //是否使用操作系统shell启动
                p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
                p.StartInfo.RedirectStandardError = true;//重定向标准错误输出

                p.Start();//启动程序

                p.StandardInput.AutoFlush = true;
                p.StandardInput.WriteLine("sc delete " + cmbservices.Text);
            }
        }
    }

    /// <summary>  
    /// 与控制台交互  
    /// </summary>  
    static class Shell
    {
        /// <summary>  
        /// 输出信息  
        /// </summary>  
        /// <param name="format"></param>  
        /// <param name="args"></param>  
        public static void WriteLine(string format, params object[] args)
        {
            WriteLine(string.Format(format, args));
        }

        /// <summary>  
        /// 输出信息  
        /// </summary>  
        /// <param name="output"></param>  
        public static void WriteLine(string output)
        {
            Console.ForegroundColor = GetConsoleColor(output);
            Console.WriteLine(@"[{0}]{1}", DateTimeOffset.Now, output);
        }

        /// <summary>  
        /// 根据输出文本选择控制台文字颜色  
        /// </summary>  
        /// <param name="output"></param>  
        /// <returns></returns>  
        private static ConsoleColor GetConsoleColor(string output)
        {
            if (output.StartsWith("警告")) return ConsoleColor.Yellow;
            if (output.StartsWith("错误")) return ConsoleColor.Red;
            if (output.StartsWith("注意")) return ConsoleColor.Green;
            return ConsoleColor.Gray;
        }
    }
}